package game.module.sss.procesor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.sss.bean.SssRound;
import game.module.sss.logic.SssDataManager;
import game.session.SessionManager;
import io.netty.buffer.ByteBuf;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageSss.C2SSssVoicePlay;
import lua.LuaMessageSss.PushSssVoicePlay;
import lua.LuaMessageSss.S2CSssVoicePlay;

@MsgCodeAnn(msgcode = C2SSssVoicePlay.id, accessLimit = 500)
public class SssVoicePlayProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(SssVoicePlayProcessor.class);

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw request) throws Exception {
		C2SSssVoicePlay reqMsg = C2SSssVoicePlay.parse(request);
		ByteBuf voice_content = reqMsg.content;
		int playerId = playingRole.getId();
		logger.info("sss voice play,playeyId={}", playerId);
		// 房间是否存在
		final SssRound ddzRound = SssDataManager.getInstance().getNnRoom(playerId);
		if (ddzRound == null) {
			S2ErrorCode retMsg = new S2ErrorCode(201);
			playingRole.writeAndFlush(retMsg.build(playingRole.alloc()));
			return;
		}
		// 发送
		S2CSssVoicePlay respMsg = new S2CSssVoicePlay();
		playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
		// 推送
		List<Integer> playerIdList = ddzRound.getPlayerIds();
		PushSssVoicePlay pushMessage = new PushSssVoicePlay();
		pushMessage.player_index = (byte) playerIdList.indexOf(playerId);
		pushMessage.content = voice_content;
		for (Integer aPlayerId : playerIdList) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushMessage.build(pr.alloc()));
			}
		}
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

}
